<template>
  <el-breadcrumb class="breadcrumb" separator="/">
    <!-- <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item
      ><a href="/">promotion management</a></el-breadcrumb-item
    > -->
    <el-breadcrumb-item v-for="(item, idx) in breadcrumbData" :key="item.path">
      <!-- 不可点击 -->
      <span class="no-redirect" v-if="idx === breadcrumbData.length - 1">{{
        item.meta.title
      }}</span>
      <!-- 可点击 -->
      <span class="redirect" v-else @click="onLinkClick(item)">{{
        item.meta.title
      }}</span>
    </el-breadcrumb-item>
    <!-- <el-breadcrumb-item>promotion detail</el-breadcrumb-item> -->
  </el-breadcrumb>
</template>

<script setup lang="ts">
import { watch, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAppStore } from '../../store'

// 获取scss文件中的变量，主题修改需要
const appStrore = useAppStore()
const linkHoverColor = appStrore.cssVar.menuBg
// 生成面包屑数组数据
let breadcrumbData = ref([])
const getBreadcrumbData = () => {
  breadcrumbData.value = route.matched.filter(
    (item) => item.meta && item.meta.title
  )
}

const route = useRoute()
watch(route, () => getBreadcrumbData(), { immediate: true })

const router = useRouter()
const onLinkClick = (data) => {
  router.push(data.path)
}
</script>

<style lang="scss" scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .redirect {
    color: #666;
    font-weight: 600;
    cursor: pointer;
  }

  .redirect:hover {
    // 将来需要进行主题替换，所以这里不去写死样式
    color: v-bind(linkHoverColor);
  }
}
</style>
